var dbutil = require("./DBUtil");

function insertFileList(fileName, fileSize, filePath,success) {
    var insertSql = "insert into file_list (file_Name, file_size, file_path) values (?, ?, ?)";
    var params = [fileName, fileSize, filePath];

    var connection = dbutil.createConnection();
    connection.connect();
    connection.query(insertSql, params, function (error, result) {
        if (error == null) {
            success(result);
        } else {
            console.log(error)
        }
    });
    connection.end();
}

function getAllFileList(page, pageSize, success) {
    var querySql = "select * from file_list order by id desc limit ?, ?;";
    var params = [page * pageSize, pageSize];

    var connection = dbutil.createConnection();
    connection.connect();
    connection.query(querySql, params, function (error, result) {
        if (error == null) {
            success(result);
        } else {
            console.log(error);
        }
    });
    connection.end();
}

function delFileList(ids, success) {
    var delSql = 'delete from file_list where id in ('+ ids +')';
    var connection = dbutil.createConnection();
    connection.connect();
    connection.query(delSql, function (error, result) {
        if (error == null) {
            success(result);
        } else {
            console.log(error);
        }
    });
    connection.end();
}

function queryFileListCount(success) {
    var querySql = "select count(1) as count from file_list;";
    var params = [];

    var connection = dbutil.createConnection();
    connection.connect();
    connection.query(querySql, params, function (error, result) {
        if (error == null) {
            success(result);
        } else {
            console.log(error);
        }
    });
    connection.end();
}

module.exports.insertFileList = insertFileList;
module.exports.getAllFileList = getAllFileList;
module.exports.delFileList = delFileList;
module.exports.queryFileListCount = queryFileListCount;